import 'dart:convert';
import 'package:after_sale_plugin/pages/models/vehicle_err_back_model.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'package:safemap/safemap.dart';
import 'package:wechat_assets_picker_plus/wechat_assets_picker_plus.dart';

class FeedbackModel {
  RxList<String> attachment = <String>[].obs;
  RxString comment = "".obs;
  // 结束反馈
  RxBool finished = true.obs;

  // map 转模型
  FeedbackModel.fromMap(Map<dynamic, dynamic> map) {
    SafeMap data = SafeMap(map);
    comment.value = data["comment"].string ?? "";
    finished.value = data["finished"].boolean;
    var list = data['attachment'].list;
    if (list != null) {
      attachment.value = list.map((e) => e as String).toList();
    }
  }

  // 模型转json
  Map<String, dynamic> toMap() {
    Map<String, dynamic> map = Map.from({});
    map["attachment"] = attachment;
    map["comment"] = comment.value;
    map["finished"] = finished.value;
    return map;
  }
}

// 解决结果
class ResolutionResult {
  RxString resolution = "".obs;
  RxList<String> attachment = <String>[].obs;
  RxString comment = "".obs;
  Rx<ResolutionStatus> state = ResolutionStatus.none.obs;

  // map 转模型
  ResolutionResult.fromMap(Map<dynamic, dynamic> map) {
    SafeMap data = SafeMap(map);
    resolution.value = data["resolution"].string ?? "";
    comment.value = data["comment"].string ?? "";
    state.value = ResolutionStatus.getTypeByNum(data["status"].toInt ?? 0) ??
        ResolutionStatus.none;
    var list = data['attachment'].list;
    if (list != null) {
      attachment.value = list.map((e) => e as String).toList();
    }
  }

  // 模型转 Map
  Map<String, dynamic> toMap() {
    Map<String, dynamic> map = Map.from({});
    map['resolution'] = resolution.value;
    map['comment'] = comment.value;
    map['status'] = state.value.rawValue;
    map['attachment'] = attachment;
    return map;
  }
}

class ReportOrderInfo {
  // sn
  String sn = "";

  // id
  String id = "";

  // 报修车型
  RxString reportVehicleName = ''.obs;

  // 报修人姓名
  RxString reportPersonName = ''.obs;

  // 报修人电话
  RxString reportPhoneNumber = ''.obs;

  // 报修类型
  RxInt reportType = (-1).obs;
  RxString reportTypeDesc = ''.obs;

  // 问题描述
  RxString problemContent = ''.obs;

  // 问题
  RxString errType = ''.obs;

  // 图片/视频
  RxList<AssetModel> assetModels = <AssetModel>[AssetModel.add].obs;

  // 反馈后提交的 - 图片/视频
  RxBool needFeedback = true.obs;
  String supplementContent = '';
  RxList<FeedbackModel> feedback = <FeedbackModel>[].obs;

  // 工单进度
  Rx<OrderProgressState> orderProgressState = OrderProgressState.submited.obs;

  // 时间
  RxInt submitedTimeStamp = 0.obs;
  String get submitedDate {
    return _dateFormat(TimeUtil.getDate(submitedTimeStamp.value));
  }

  // 处理时间
  RxInt startedTimeStamp = 0.obs;
  String get startedDate {
    return _dateFormat(TimeUtil.getDate(startedTimeStamp.value));
  }

  // 完成时间
  RxInt completeTimeStamp = 0.obs;
  String get completeDate {
    return _dateFormat(TimeUtil.getDate(completeTimeStamp.value));
  }

  // 关闭时间
  RxInt closedTimeStamp = 0.obs;
  String get closedDate {
    return _dateFormat(TimeUtil.getDate(closedTimeStamp.value));
  }

  String _dateFormat(DateTime date) {
    String year = StringUtil.padLeft(2, '${date.year}', '0');
    String month = StringUtil.padLeft(2, '${date.month}', '0');
    String day = StringUtil.padLeft(2, '${date.day}', '0');
    String hour = StringUtil.padLeft(2, '${date.hour}', '0');
    String minute = StringUtil.padLeft(2, '${date.minute}', '0');
    return "$year/$month/$day $hour:$minute";
  }

  // 处理结果
  Rx<ResolutionResult> resolutionResult = ResolutionResult.fromMap({}).obs;

  // 车辆检测的错误信息
  Rx<DiagnosisModel> diagnosis = DiagnosisModel.fromMap({}).obs;

  // map转模型
  ReportOrderInfo.fromMap(Map<dynamic, dynamic> map) {
    SafeMap data = SafeMap(map);
    sn = data['sn'].string ?? '';
    id = data['id'].string ?? '';
    reportVehicleName.value = data['device_name'].string?.replaceAll(RegExp(r'\x00'), '') ?? '';
    reportPersonName.value = data['user_name'].string ?? '';
    reportPhoneNumber.value = data['phone'].string ?? '';
    if (data['type'].toInt != null) {
      reportType.value = data['type'].toInt!;
      reportTypeDesc.value =
          ReportType.getTypeByNum(reportType.value)?.title ?? "";
    }
    orderProgressState.value =
        OrderProgressState.getTypeByNum(data['status'].toInt ?? 0) ??
            OrderProgressState.closed;
    submitedTimeStamp.value = data['submitted_time'].toInt ?? 0;
    startedTimeStamp.value = data['started_time'].toInt ?? 0;
    completeTimeStamp.value = data['finished_time'].toInt ?? 0;
    closedTimeStamp.value = data['closed_time'].toInt ?? 0;
    problemContent.value = data['content'].string ?? '';
    errType.value = data['err_type'].string ?? '';
    if (data['result'].map != null) {
      resolutionResult.value = ResolutionResult.fromMap(data['result'].map!);
    }
    var list = data['attachment'].list;
    if (list != null) {
      assetModels.value = list.map((e) {
        if (e is String) {
          return AssetModel.fromMap({})..src = e;
        } else {
          return AssetModel.fromMap(e);
        }
      }).toList();
    }
    var fList = data['feedback'].list;
    if (fList != null) {
      feedback.value = fList.map((e) {
        return FeedbackModel.fromMap(e);
      }).toList();
      if (feedback.isNotEmpty) {
        needFeedback.value = !feedback.first.finished.value;
      }
    }

    Map<dynamic, dynamic>? d = data['diagnosis'].map;
    if (d != null) {
      diagnosis.value = DiagnosisModel.fromMap(d);
    }

    // 补充的内容
    supplementContent = data['feedback_content'].string ?? '';
  }

  // 模型转json
  String toJson() {
    Map<String, dynamic> map = Map.from({});
    map['sn'] = sn;
    map['id'] = id;
    map['device_name'] = reportVehicleName.value;
    map['user_name'] = reportPersonName.value;
    map['phone'] = reportPhoneNumber.value;
    map['type'] = reportType.value;
    map['content'] = problemContent.value;
    map['err_type'] = errType.value;
    map['feedback_content'] = supplementContent;
    List<Map<String, dynamic>> list = [];
    for (var i = 0; i < assetModels.length; i++) {
      if (!assetModels[i].isAdd) {
        list.add(assetModels[i].toMap());
      }
    }
    map['attachment'] = list;

    List<Map<String, dynamic>> fList = [];
    for (var i = 0; i < feedback.length; i++) {
      fList.add(feedback[i].toMap());
    }
    map['feedback'] = fList;

    map['result'] = resolutionResult.value.toMap();

    map['diagnosis'] = diagnosis.value.toMap();
    return jsonEncode(map);
  }
}
